using System;
using System.Collections.Generic;
using Assets;
using HarmonyLib;
using PegasusLettuce;

namespace Mercenary
{
    public static class UtilsCom
    {
        public static void GotoScenePlay()
        {
            SceneMgr.Get().SetNextMode(SceneMgr.Mode.LETTUCE_PLAY,
                SceneMgr.TransitionHandlerType.NEXT_SCENE, null, null);
        }

        public static void GotoSceneMap()
        {
            SceneMgr.Get().SetNextMode(SceneMgr.Mode.LETTUCE_MAP,
                SceneMgr.TransitionHandlerType.NEXT_SCENE, null, null);
        }

        public static void GotoSceneVillage()
        {
            SceneMgr.Get().SetNextMode(SceneMgr.Mode.LETTUCE_VILLAGE,
                SceneMgr.TransitionHandlerType.SCENEMGR, null, null);
        }

        //获取打出的可选中卡牌，即场面卡牌（可选中，隐身等除外）
        public static List<Card> GetValidTargetPlayCard(Player.Side side)
        {
            return ZoneMgr.Get().FindZoneOfType<ZonePlay>(side).
                GetCards().FindAll((Card card) => 
                (card.GetActor().GetActorStateType() == ActorStateType.CARD_VALID_TARGET || 
                card.GetActor().GetActorStateType() == ActorStateType.CARD_VALID_TARGET_MOUSE_OVER) &&
                !card.GetEntity().IsStealthed());
        }

        //获取场面所有卡牌（隐身等也包含）
        public static List<Card> GetAllPlayCard(Player.Side side)
        {
            return ZoneMgr.Get().FindZoneOfType<ZonePlay>(side).GetCards();
        }

        //获取手牌
        public static List<Card> GetAllHandCard(Player.Side side)
        {
            return ZoneMgr.Get().FindZoneOfType<ZoneHand>(side).GetCards();
        }

        /*
        //构建可选佣兵
        public static List<MercCard> BuildValidTargetMerc(Player.Side side)
        {
            List<MercCard> list = new List<MercCard>();
            foreach (Card card in GetValidTargetPlayCard(side))
            {
                Entity entity = card.GetEntity();
                MercCard item = new MercCard(
                    entity.GetName(),entity.GetEntityId(),entity.GetCurrentHealth(),
                    card.GetPreparedLettuceAbilitySpeedValue(),entity.GetDefHealth());
                list.Add(item);
            }
            return list;
        }

        //构建所有友方佣兵
        public static List<MercCard> BuildAllFriendlyTargetMerc()
        {
            List<Card> cards = GetAllPlayCard(Player.Side.FRIENDLY);
            List<MercCard> list = new List<MercCard>();
            foreach (Card card in cards)
            {
                Entity entity = card.GetEntity();
                MercCard mercenary = new MercCard(entity.GetName());
                List<MercSkill> list2 = new List<MercSkill>();
                foreach (int id in card.GetEntity().GetLettuceAbilityEntityIDs())
                {
                    Entity entity2 = GameState.Get().GetEntity(id);
                    if (entity2 != null && !entity2.IsLettuceEquipment())
                    {
                        list2.Add(new MercSkill(entity2.GetEntityId(),
                            entity2.GetName().Substring(0, entity2.GetName().Length-1)));
                    }
                }
                mercenary.m_skills = list2;
                list.Add(mercenary);
            }
            return list;
        }
        */

        //构建佣兵
        public static List<MercCard> BuildMercFromCard(List<Card> cards)
        {
            List<MercCard> list = new List<MercCard>();
            foreach (Card card in cards)
            {
                Entity entity = card.GetEntity();
                MercCard mercenary = new MercCard();
                mercenary.m_id = entity.GetEntityId();
                mercenary.m_name = entity.GetName();
                list.Add(mercenary);
            }
            return list;
        }

        //构建佣兵
        public static List<Target> BuildTargetFromCards(List<Card> cards, Player.Side side)
        {
            List<Target> list = new List<Target>();
            foreach (Card card in cards)
            {
                bool flag_avalue = card.GetActor().GetActorStateType() == ActorStateType.CARD_VALID_TARGET || card.GetActor().GetActorStateType() == ActorStateType.CARD_VALID_TARGET_MOUSE_OVER;
                if (side != Player.Side.FRIENDLY)
                    flag_avalue = flag_avalue && !card.GetEntity().IsStealthed();
                List<Skill> skills = new List<Skill>();
                foreach (int id in card.GetEntity().GetLettuceAbilityEntityIDs())
                {
                    Entity entity2 = GameState.Get().GetEntity(id);
                    if (entity2 != null && !entity2.IsLettuceEquipment())
                    {
                        //有些技能没有等级，不删掉后面的数字
                        string tmpName = entity2.GetName();
                        if (tmpName.Length > 0 &&
                            Char.IsNumber(tmpName[tmpName.Length - 1]))
                            tmpName = entity2.GetName().Substring(0, entity2.GetName().Length - 1);
                        skills.Add(new Skill
                        {
                            Name = tmpName,
                            Id = entity2.GetEntityId()
                        });
                    }
                }
                Target item = new Target
                {
                    Name = card.GetEntity().GetName(),
                    Id = card.GetEntity().GetEntityId(),
                    Attack = card.GetEntity().GetATK(),
                    Health = card.GetEntity().GetCurrentHealth(),
                    Speed = card.GetPreparedLettuceAbilitySpeedValue(),
                    DefHealth = card.GetEntity().GetDefHealth(),
                    Role = (TAG_ROLE)(card.GetEntity().GetTag<TAG_ROLE>(GAME_TAG.LETTUCE_ROLE)),
                    Enable = flag_avalue,
                    Skills = skills,
                };
                list.Add(item);
            }
            return list;
        }
    }
}
